home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / amc / amc_install.exe / {app} / Scripts / StringUtils1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2005-01-02  |  3.3 KB  |  115 lines

  1. unit StringUtils1;
  2.  
  3. {
  4.   PLEASE READ THIS BEFORE MODIFYING THIS FILE :
  5.   If you want to put some of your functions in an external
  6.   file because they are common to several of your scripts,
  7.   please create a new file your your functions
  8.   (e.g. StringUtils2 if they are also functions to work on
  9.   strings) rather than add them in this file.
  10.   So each "unit" (.pas files) belong to one script creator
  11.   and there is no risk that two people modify it at the
  12.   same time.
  13. }
  14.  
  15. {
  16.   This file was created by Antoine Potten, originally
  17.   for IMDB script.
  18.   Of course, you can use these functions in your scripts :
  19.   simply add "StringUtils1" in the uses clause of your
  20.   script.
  21. }
  22.  
  23. var
  24.   RemainingText: string;
  25.   {
  26.     when calling...     this variable contains...
  27.     TextBefore          the text after SearchText
  28.     TextBetween         the text after AfterText
  29.   }
  30.  
  31. // ***** Like the Pos function, but returns the last occurence instead of the first one *****
  32.  
  33. function LastPos(ASearch: string; AText: string): Integer;
  34. var
  35.   CurPos, PrevPos: Integer;
  36. begin
  37.   PrevPos := 0;
  38.   CurPos := Pos(ASearch, AText);
  39.   while CurPos > 0 do
  40.   begin
  41.     if PrevPos = 0 then
  42.       PrevPos := CurPos
  43.     else
  44.       PrevPos := PrevPos + CurPos + Length(ASearch) - 1;
  45.     Delete(AText, 1, CurPos + Length(ASearch) - 1);
  46.     CurPos := Pos(ASearch, AText);
  47.   end;
  48.   Result := PrevPos;
  49. end;
  50.  
  51. // *****
  52. {    Returns the text before SearchText, but not before BeginLimit (if it is not empty),
  53.     It takes the last occurence of BeginLimit found before the position of SearchText  }
  54.  
  55. function TextBefore(WholeText: string; SearchText: string; BeginLimit: string): string;
  56. var
  57.   FoundPos, PrevPos: Integer;
  58.   WorkText: string;
  59. begin
  60.   Result := '';
  61.   FoundPos := Pos(SearchText, WholeText);
  62.   if FoundPos = 0 then
  63.     Exit;
  64.   WorkText := Copy(WholeText, 1, FoundPos - 1);
  65.   RemainingText := Copy(WholeText, FoundPos + Length(SearchText), Length(WholeText));
  66.   if BeginLimit <> '' then
  67.   begin
  68.     FoundPos := LastPos(BeginLimit, WorkText);
  69.     if FoundPos = 0 then
  70.       Exit
  71.     else
  72.       FoundPos := FoundPos + Length(BeginLimit);
  73.   end
  74.   else
  75.     FoundPos := 1;
  76.   Result := Copy(WorkText, FoundPos, Length(WorkText));
  77. end;
  78.  
  79. // ***** Returns the text after SearchText *****
  80.  
  81. function TextAfter(WholeText: string; SearchText: string): string;
  82. var
  83.   FoundPos: Integer;
  84. begin
  85.   Result := '';
  86.   FoundPos := Pos(SearchText, WholeText);
  87.   if FoundPos = 0 then
  88.     Exit;
  89.   Result := Copy(WholeText, FoundPos + Length(SearchText), Length(WholeText));
  90. end;
  91.  
  92. // *****
  93. {    Returns the text between BeforeText and AfterText (without these two strings),
  94.      It takes the first AfterText occurence found after the position of BeforeText  }
  95.  
  96. function TextBetween(WholeText: string; BeforeText: string; AfterText: string): string;
  97. var
  98.   FoundPos: Integer;
  99.   WorkText: string;
  100. begin
  101.   Result := '';
  102.   FoundPos := Pos(BeforeText, WholeText);
  103.   if FoundPos = 0 then
  104.     Exit;
  105.   WorkText := Copy(WholeText, FoundPos + Length(BeforeText), Length(WholeText));
  106.   FoundPos := Pos(AfterText, WorkText);
  107.   if FoundPos = 0 then
  108.     Exit;
  109.   Result := Copy(WorkText, 1, FoundPos - 1);
  110.   RemainingText := Copy(WorkText, FoundPos + Length(AfterText), Length(WorkText));
  111. end;
  112.  
  113. begin
  114. end.
  115.